<?php
/**
 * @author jim
 * @date 2017/11/25
 */

namespace app\admin\controller;
use app\admin\model\JsCustomer;
use app\admin\model\StockApply;
use app\admin\model\StockMoneyAllot;
class Stockholder extends Common{
    //股东列表
    public function index() {
        if($_POST || !$_GET['page']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['uname']  = @$_POST['uname'] ? $_POST['uname'] : @$sessionParam['uname'];
        $sessionArr['tname']  = @$_POST['tname'] ? $_POST['tname'] : @$sessionParam['tname'];
        $sessionArr['status'] = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['page'] ? $_GET['page'] : 1;
        $where = [];
        $where['approvestateid'] = 4;
        $customerModel = new JsCustomer();
        if($sessionArr['uname']){
            $uRes = $customerModel->findData(['Username'=>$sessionArr['uname']]);
            $where['CustomerId'] = $uRes['Id'];
        }
        if($sessionArr['tname']){
            $uRes = $customerModel->findData(['TrueName'=>$sessionArr['tname']]);
            $where['CustomerId'] = $uRes['Id'];
        }

        if($sessionArr['status'] == "申请时间")
        {
            $dateField ='CreateOn';
        }
        else
        {
            $dateField ='CheckedOn';
        }

        if($sessionArr['start'] && $sessionArr['end']){
            $where[$dateField] = ['between',[$sessionArr['start'],$sessionArr['end'].' 23:59:59']];
        }
        $where['CompanyId'] = session('company_id');
        $saModel = new StockApply();
        $resList = $saModel->searchData($where,$page,$this->pernum,'Id desc');
          $uidArr = [];
          foreach ($resList as $val){
              $uidArr[] = $val['CustomerId'];
          }
          $uRes = [];

          if($uidArr){
              $uList = $customerModel->selectData(['Id'=>['in',$uidArr]],'Id,TrueName,Username');
              foreach ($uList as $val){
                  $uRes[$val['Id']]['tname'] = $val['TrueName'];
                  $uRes[$val['Id']]['uname'] = $val['Username'];
              }
          }
        $totalNum = $saModel->getTotalNum($where);
        $pageShow = $saModel->paginate($this->pernum,$totalNum)->render();
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('postArr',$sessionArr);
        $this->assign('uRes',$uRes);
        $tmpdata =  array('1'=>'省级股东','2'=>'市级股东','3'=>'县区级股东');
        $this->assign('stockRes',$tmpdata);
        return $this->fetch();
    }


    //分红明细
    public function stockdetailed(){
        $id = $_GET['id'];
        $cid = $_GET['cid'];
        $page = @$_GET['page'] ? $_GET['page'] : 1;
        $CustomerModel = new JsCustomer();
        $uRes = $CustomerModel->findData(['Id'=>$cid]);
        $startNum = ($page-1)*$this->pernum;

        $sqldata = "SELECT b.stockTypeId,FORMAT((b.stockNum / 100),2) AS stockratio,b.stockNum,a.uid,a.money,a.maid,a.addtime,c.allot_num FROM stockapply b ";
        $sqlcount = "SELECT count(0) as num FROM stockapply b ";
        $sql = " LEFT JOIN stock_money_allot a ON b.id = a.said ";
        $sql .= " LEFT JOIN company_money_allot c ON a.maid = c.id ";
        $sql .= " WHERE b.CustomerId = {$cid} AND a.said= {$id} AND b.approvestateid=4 ";
        $sqlOrderby = " ORDER BY c.allot_num DESC LIMIT {$startNum} ,{$this->pernum}";

        $strckMoneyAllotModel = new StockMoneyAllot();
        $thList = $strckMoneyAllotModel->queryData($sqldata.$sql.$sqlOrderby);
        $totalNum = $strckMoneyAllotModel->queryData($sqlcount.$sql);
        $pageShow = $strckMoneyAllotModel->paginate($this->pernum,$totalNum[0]['num'],['query'=>['id'=>$id]])->render();
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$thList);
        $this->assign('uRes',$uRes);
        $tmpdata =  array('1'=>'省级股东','2'=>'市级股东','3'=>'县区级股东');
        $this->assign('stockRes',$tmpdata);
        $this->view->engine->layout(false);
        return $this->fetch();
    }
}